#!/usr/bin/env python
# coding=utf-8
#
# 添加新的Cron Job

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import util
import crondb
import common
#import sys

class CreateCronJobHandler(webapp.RequestHandler):
	def get(self):
		user = users.get_current_user()
		if user:
			if users.is_current_user_admin():
				# get post data
				self.writeHtmlHeader()
				self.writeLoopFooter()
			else:
				common.writeNoPermissionMessage(self)
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def post(self):
		user = users.get_current_user()
		if user:
			if users.is_current_user_admin():
				# get post data
				title = self.request.get("title")
				url = self.request.get("url")
				sendOkMsg = bool(self.request.get("sendOkMsg"))
				sendFailMsg = bool(self.request.get("sendFailMsg"))
				enabled = bool(self.request.get("enabled"))
				minutes = self.request.get("minutes", allow_multiple=True)
				hours = self.request.get("hours", allow_multiple=True)
				dayOfMonth = self.request.get("dayOfMonth", allow_multiple=True)
				dayOfWeek = self.request.get("dayOfWeek", allow_multiple=True)
				month = self.request.get("month", allow_multiple=True)
				cron = crondb.CronInfo(
					title=title,
					url=url,
					sendSuccessMsg=sendOkMsg,
					sendFailMsg=sendFailMsg,
					enabled=enabled,
					minutes=common.toLongList(minutes),
					hours=common.toLongList(hours),
					dayOfMonth=common.toLongList(dayOfMonth),
					dayOfWeek=common.toLongList(dayOfWeek),
					month=common.toLongList(month)
				)
				crondb.addCronInfo(cron)
				
				self.redirect('cronlist')
			else:
				common.writeNoPermissionMessage(self)
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def writeHtmlHeader(self):
		self.response.out.write("""<html>
<head>
    <title>%s</title>
    <link href="css/general.css" rel="stylesheet" type="text/css" />
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <script src="js/global.js" type="text/javascript" language="javascript"> </script>
</head>
<body>
<form action="/createcron" name="f" method="post">
<div>
<h1>
	<span class="action-span"><a href="cronlist">Cron Job List</a></span><span class="action-span1"><a
		href="main">Beikx Cron</a> - Create Cron Job </span>
</h1>
<div style="clear: both" />
<div class="list-div">
	<div style="padding:5px;">
	<label for="title" style="width:50px;display:block;float:left;">
		Title:</label> <input id="title" name="title" type="text" value="" style="width:500px;" />
	</div>
	<div style="padding:5px;">
	<label for="url" style="width:50px;display:block;float:left;">
		Url:</label> <input id="url" name="url" type="text" value="" style="width:500px;" /> &nbsp;&nbsp;<span style="color:gray;font-size:10px;">Start with 'http://'. example: <a href="http://www.beikx.com" target="_blank">http://www.beikx.com/cron.php</a></span>
	</div>
	<div style="padding:5px;">
	<input id="sendOkMsg" name="sendOkMsg" type="checkbox" value="1" />
	<label for="sendOkMsg">
		Send the returned message, when cron job work done.</label>
	</div>
	<div style="padding:5px;">
	<input id="sendFailMsg" name="sendFailMsg" type="checkbox" value="2" />
	<label for="sendFailMsg">
		Send the error message, when cron job work fail.</label>
	</div>
	<div style="padding:5px;">
	<input id="enabled" name="enabled" type="checkbox" value="3" />
	<label for="enabled">
		enabled</label>
	</div>
	<div style="padding:5px; height:100px;">
	<div class="mulsel_div">
	<label for="minutes" style="width:120px;display:block;">
		Minutes:</label> 
		<select id="minutes" name="minutes" multiple>
		<option value="-1" selected>All</option>
		<option value="0">xx:00</option>
		<option value="1">xx:05</option>
		<option value="2">xx:10</option>
		<option value="3">xx:15</option>
		<option value="4">xx:20</option>
		<option value="5">xx:25</option>
		<option value="6">xx:30</option>
		<option value="7">xx:35</option>
		<option value="8">xx:40</option>
		<option value="9">xx:45</option>
		<option value="10">xx:50</option>
		<option value="11">xx:55</option>
		</select>
	</div>
	<div class="mulsel_div">
	<label for="hours" style="width:120px;display:block;">
		Hours:</label> 
		<select id="hours" name="hours" multiple>
		<option value="-1" selected>All</option>
		<option value="0">00:xx</option>
		<option value="1">01:xx</option>
		<option value="2">02:xx</option>
		<option value="3">03:xx</option>
		<option value="4">04:xx</option>
		<option value="5">05:xx</option>
		<option value="6">06:xx</option>
		<option value="7">07:xx</option>
		<option value="8">08:xx</option>
		<option value="9">09:xx</option>
		<option value="10">10:xx</option>
		<option value="11">11:xx</option>
		<option value="12">12:xx</option>
		<option value="13">13:xx</option>
		<option value="14">14:xx</option>
		<option value="15">15:xx</option>
		<option value="16">16:xx</option>
		<option value="17">17:xx</option>
		<option value="18">18:xx</option>
		<option value="19">19:xx</option>
		<option value="20">20:xx</option>
		<option value="21">21:xx</option>
		<option value="22">22:xx</option>
		<option value="23">23:xx</option>
		</select>
	</div>
	<div class="mulsel_div">
	<label for="dayOfMonth" style="width:120px;display:block;">
		Day of Month:</label> 
		<select id="dayOfMonth" name="dayOfMonth" multiple>
		<option value="-1" selected>All</option>
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
		<option value="13">13</option>
		<option value="14">14</option>
		<option value="15">15</option>
		<option value="16">16</option>
		<option value="17">17</option>
		<option value="18">18</option>
		<option value="19">19</option>
		<option value="20">20</option>
		<option value="21">21</option>
		<option value="22">22</option>
		<option value="23">23</option>
		<option value="24">24</option>
		<option value="25">25</option>
		<option value="26">26</option>
		<option value="27">27</option>
		<option value="28">28</option>
		<option value="29">29</option>
		<option value="30">30</option>
		<option value="31">31</option>
		</select>
	</div>
	<div class="mulsel_div">
	<label for="dayOfWeek" style="width:120px;display:block;">
		Day of Week:</label> 
		<select id="dayOfWeek" name="dayOfWeek" multiple>
		<option value="-1" selected>All</option>
		<option value="0">Sunday</option>
		<option value="1">Monday</option>
		<option value="2">Tuesday</option>
		<option value="3">Wednesday</option>
		<option value="4">Thursday</option>
		<option value="5">Friday</option>
		<option value="6">Saturday</option>
		</select>
	</div>
	<div class="mulsel_div">
	<label for="month" style="width:120px;display:block;">
		Month:</label> 
		<select id="month" name="month" multiple>
		<option value="-1" selected="">All</option>
		<option value="1">January</option>
		<option value="2">February</option>
		<option value="3">March</option>
		<option value="4">April</option>
		<option value="5">May</option>
		<option value="6">June</option>
		<option value="7">July</option>
		<option value="8">August</option>
		<option value="9">September</option>
		<option value="10">October</option>
		<option value="11">November</option>
		<option value="12">December</option>
		</select>
	</div>
	</div>
	<div style="clear: both" />
	<div style="padding:5px;padding-left:30px;">
		<input id="submit" name="submit" type="submit" value="Save" CssClass="button" onclick="javascript:return submitFrm();" />
		&nbsp;&nbsp;<input id="reset" name="reset" type="reset" value="Reset" CssClass="button" />
	</div>
</div>
</div>
</form>
""" % common.getAppName())

	def writeLoopFooter(self):
		self.response.out.write("""
<br />
<div id="footer">
	<br />
	%s
</div>
</div>""" % common.getCopyRight())
		self.response.out.write(self.getFooterJs())
		self.response.out.write("</body></html>")

	def getFooterJs(self):
		return """<script type="text/javascript" language="javascript">
<!-- 
function submitFrm()
{
	var obj = $('title');
	if (obj) {
		if (obj.value == '') {
			alert("Please input title.");
			obj.focus();
			return false;
		}
	}
	obj = $('url');
	if (obj) {
		if (obj.value == '') {
			alert("Please input url.");
			obj.focus();
			return false;
		} else {
			var url = obj.value;
			if (!((/^http:\\/\\/.+/ig).test(url))) {
				alert("The url should be like 'http://www.beikx.com/cron.php'.");
				obj.focus();
				return false;
			}
		}
	}
	return true;
}
//-->
</script>"""


def main():
	#code = sys.getdefaultencoding()
	#if code != 'utf8':
	#	reload(sys)
	#	sys.setdefaultencoding('utf8')
	application = webapp.WSGIApplication([('/createcron', CreateCronJobHandler)],
		debug=True)
	util.run_wsgi_app(application)


if __name__ == '__main__':
	main()
